home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / grafica / amhelios / c_jitter.cpp < prev    next >
C/C++ Source or Header  |  1999-01-01  |  4KB  |  151 lines

  1. ////////////////////////////////////////////////////////////
  2. //
  3. //  C_JITTER.CPP - Color Reduction Filter Class
  4. //
  5. //  Version:    1.03A
  6. //
  7. //  History:    94/08/23 - Version 1.00A release.
  8. //              94/12/16 - Version 1.01A release.
  9. //              95/02/05 - Version 1.02A release.
  10. //              95/07/21 - Version 1.02B release.
  11. //              96/02/14 - Version 1.02C release.
  12. //              96/04/01 - Version 1.03A release.
  13. //
  14. //  Compilers:  Microsoft Visual C/C++ Professional V1.5
  15. //              Borland C++ Version 4.5
  16. //
  17. //  Author:     Ian Ashdown, P.Eng.
  18. //              byHeart Software Limited
  19. //              620 Ballantree Road
  20. //              West Vancouver, B.C.
  21. //              Canada V7S 1W3
  22. //              Tel. (604) 922-6148
  23. //              Fax. (604) 987-7621
  24. //
  25. //  Copyright 1994-1996 byHeart Software Limited
  26. //
  27. //  The following source code has been derived from:
  28. //
  29. //    Ashdown, I. 1994. Radiosity: A Programmer's
  30. //    Perspective. New York, NY: John Wiley & Sons.
  31. //
  32. //  It may be freely copied, redistributed, and/or modified
  33. //  for personal use ONLY, as long as the copyright notice
  34. //  is included with all source code files.
  35. //
  36. ////////////////////////////////////////////////////////////
  37.  
  38. // Adapted from: Bragg, D. [1992]. "A Simple Color Reduction
  39. //               Filter", in Graphics Gems III (D. Kirk,
  40. //               Ed.), Academic Press, San Diego, CA, 20 -
  41. //               22, 429 - 431
  42.  
  43. #include "c_jitter.h"
  44.  
  45. ColorJitter::ColorJitter()      // Class constructor
  46. {
  47.   int i;    // Loop index
  48.  
  49.   status = TRUE;
  50.  
  51.   // Initialize jitter lookup table pointers
  52.   pirand = NULL;
  53.   pxrand = NULL;
  54.   pyrand = NULL;
  55.  
  56.   noise = 1;    // Set default noise level
  57.  
  58.   // Allocate jitter lookup tables
  59.   if ((pirand = new int[C_TableSize]) == NULL)
  60.   {
  61.     status = FALSE;
  62.     return;
  63.   }
  64.  
  65.   if ((pxrand = new double[C_TableSize]) == NULL)
  66.   {
  67.     status = FALSE;
  68.     return;
  69.   }
  70.  
  71.   if ((pyrand = new double[C_TableSize]) == NULL)
  72.   {
  73.     status = FALSE;
  74.     return;
  75.   }
  76.  
  77.   // Initialize jitter lookup tables
  78.   for (i = 0; i < C_TableSize; i++)
  79.   {
  80.     pirand[i] = (int) ((double) C_TableSize * ((double)
  81.         (rand() % C_LargeNum) / (double) C_LargeNum));
  82.     pxrand[i] = (double) (rand() % C_LargeNum) / (double)
  83.         C_LargeNum;
  84.     pyrand[i] = (double) (rand() % C_LargeNum) / (double)
  85.         C_LargeNum;
  86.   }
  87. }
  88.  
  89. ColorJitter::~ColorJitter()     // Class destructor
  90. {
  91.   // Release jitter lookup tables
  92.   if (pirand != NULL)
  93.     delete [] pirand;
  94.  
  95.   if (pxrand != NULL)
  96.     delete [] pxrand;
  97.  
  98.   if (pyrand != NULL)
  99.     delete [] pyrand;
  100. }
  101.  
  102. // Perform color reduction by jittering color values
  103. void ColorJitter::Reduce( ColorRGB *pc, int x, int y )
  104. {
  105.   int i;            // Loop index
  106.   int p, q;         // Temporary variables
  107.   BYTE color[3];    // Color band values
  108.  
  109.   // Get color band values
  110.   color[0] = pc->GetRed();
  111.   color[1] = pc->GetGreen();
  112.   color[2] = pc->GetBlue();
  113.  
  114.   for (i = 0; i < 3; i++)
  115.   {
  116.     if (color[i] < 248)
  117.     {
  118.       // Map color band value to one of 32 possible output
  119.       // values and determine remainder
  120.       p = (int) (color[i] % 8);
  121.  
  122.       // Look up random jitter value based on color band
  123.       // index and pixel x-y co-ordinates
  124.       q = (int) (JitterX(x, y, i) * 9.0);
  125.  
  126.       // Jitter color band value
  127.       if (p >= q)
  128.         color[i] += 8;
  129.  
  130.       // Calculate second jitter value and add to color
  131.       // band value
  132.       q = 8 * ((int) ((JitterY(x, y, i) * (double) (2 *
  133.           noise)) + 0.5) - noise) + (int) color[i];
  134.  
  135.       // Ensure jittered color band value is within
  136.       // allowable range
  137.       if (q >= 0 && q <= 255)
  138.         color[i] = (BYTE)q;
  139.     }
  140.  
  141.     // Mask off lowest three bits to create 5-bit value
  142.     color[i] &= 0xf8;
  143.   }
  144.  
  145.   // Set jittered color band values
  146.   pc->SetRed(color[0]);
  147.   pc->SetGreen(color[1]);
  148.   pc->SetBlue(color[2]);
  149. }
  150.  
  151.